﻿using CommandLinex;
using N_m3u8DL_RE.Entity;
using N_m3u8DL_RE.Enums;
using N_m3u8DL_RE.Util;
using System.Text.RegularExpressions;

namespace N_m3u8DL_RE.CommandLinex
{
    public partial class StreamFilterValueProcessor : ICommandLineValueProcessor
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public object Process(string value, Type targetType)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            var streamFilter = new StreamFilter();
            var input = value;
            var p = new ComplexParamParser(input);


            // 目标范围
            var forStr = "";
            if (input == StreamFilterRegex.ForStrRegex().Match(input).Value)
            {
                forStr = input;
            }
            else
            {
                forStr = p.GetValue("for") ?? "best";
                if (forStr != StreamFilterRegex.ForStrRegex().Match(forStr).Value)
                {
                    throw new Exception($"for={forStr} not valid");
                }
            }
            streamFilter.For = forStr;

            var id = p.GetValue("id");
            if (!string.IsNullOrEmpty(id))
                streamFilter.GroupIdReg = new Regex(id);

            var lang = p.GetValue("lang");
            if (!string.IsNullOrEmpty(lang))
                streamFilter.LanguageReg = new Regex(lang);

            var name = p.GetValue("name");
            if (!string.IsNullOrEmpty(name))
                streamFilter.NameReg = new Regex(name);

            var codecs = p.GetValue("codecs");
            if (!string.IsNullOrEmpty(codecs))
                streamFilter.CodecsReg = new Regex(codecs);

            var res = p.GetValue("res");
            if (!string.IsNullOrEmpty(res))
                streamFilter.ResolutionReg = new Regex(res);

            var frame = p.GetValue("frame");
            if (!string.IsNullOrEmpty(frame))
                streamFilter.FrameRateReg = new Regex(frame);

            var channel = p.GetValue("channel");
            if (!string.IsNullOrEmpty(channel))
                streamFilter.ChannelsReg = new Regex(channel);

            var range = p.GetValue("range");
            if (!string.IsNullOrEmpty(range))
                streamFilter.VideoRangeReg = new Regex(range);

            var url = p.GetValue("url");
            if (!string.IsNullOrEmpty(url))
                streamFilter.UrlReg = new Regex(url);

            var segsMin = p.GetValue("segsMin");
            if (!string.IsNullOrEmpty(segsMin))
                streamFilter.SegmentsMinCount = long.Parse(segsMin);

            var segsMax = p.GetValue("segsMax");
            if (!string.IsNullOrEmpty(segsMax))
                streamFilter.SegmentsMaxCount = long.Parse(segsMax);

            var plistDurMin = p.GetValue("plistDurMin");
            if (!string.IsNullOrEmpty(plistDurMin))
                streamFilter.PlaylistMinDur = OtherUtil.ParseSeconds(plistDurMin);

            var plistDurMax = p.GetValue("plistDurMax");
            if (!string.IsNullOrEmpty(plistDurMax))
                streamFilter.PlaylistMaxDur = OtherUtil.ParseSeconds(plistDurMax);

            var bwMin = p.GetValue("bwMin");
            if (!string.IsNullOrEmpty(bwMin))
                streamFilter.BandwidthMin = int.Parse(bwMin) * 1000;

            var bwMax = p.GetValue("bwMax");
            if (!string.IsNullOrEmpty(bwMax))
                streamFilter.BandwidthMax = int.Parse(bwMax) * 1000;

            var role = p.GetValue("role");
            if (System.Enum.TryParse(role, true, out RoleType roleType))
                streamFilter.Role = roleType;

            return streamFilter;
        }
    }
}
